package com.tmc.service.baseinfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.common.Pagination;
import com.tmc.constant.CacheConstant;
import com.tmc.dao.impl.EthAgentDaoImpl;
import com.tmc.entity.common.EthAgent;
import com.tmc.util.ObjectUtil;

@Service
@Transactional
public class EthAgentServiceImpl {

	@Autowired
	private EthAgentDaoImpl dao;

	private Log logger = LogFactory.getLog(this.getClass());

	public Pagination<EthAgent> findByCon(ContextInfo context, EthAgent cond, int pageIndex, int pageSize) {
		Pagination<EthAgent> result = dao.findByCon(context, cond, pageIndex, pageSize);
		return result;
	}

	@Cacheable(value = CacheConstant.CACHE_NAME_600, key = "'KEY_AGENT_ALL'")
	public List<EthAgent> findAllFromCache(ContextInfo context) {
		return dao.findAll(context);
	}

	public Map<String, EthAgent> findMapFromCache(ContextInfo context) {
		return this.toMap(this.findAllFromCache(context));
	}

	private Map<String, EthAgent> toMap(List<EthAgent> vendors) {
		Map<String, EthAgent> map = new HashMap<String, EthAgent>();
		for (EthAgent vendor : vendors) {
			map.put(vendor.getAgentCode(), vendor);
		}
		return map;
	}

	@CacheEvict(value = CacheConstant.CACHE_NAME_600, key = "'KEY_AGENT_ALL'")
	public CommonResult delete(ContextInfo context, Long vendorId) {
		EthAgent vendor = dao.getById(vendorId);
		if (vendor != null) {
			ObjectUtil.updateBaseInfo(vendor, context);
			vendor.setEnabledFlag("N");
			dao.update(vendor);
		}
		return CommonResult.makeSuccess("删除成功！");
	}

	public EthAgent getById(ContextInfo context, Long vendorId) {
		return dao.getById(vendorId);
	}

	@Deprecated
	public List<EthAgent> findAll(ContextInfo context) {
		return dao.findAll(context);
	}

	@CacheEvict(value = CacheConstant.CACHE_NAME_600, key = "'KEY_AGENT_ALL'")
	public CommonResult save(ContextInfo context, EthAgent vendor) {
		CommonResult check = this.check(vendor);
		if (!check.isSuccess()) {
			return check;
		}
		// 保存
		if (vendor.getAgentId() == null) {
			EthAgent dbEthAgent = dao.getByCode(context, vendor.getAgentCode());
			if (dbEthAgent != null) {
				return CommonResult.makeFail("已存在相同代理人");
			}
			ObjectUtil.setBaseInfo(vendor, context);
			dao.insert(vendor);
		} else {
			// 编辑
			try {
				EthAgent dbEthAgent = dao.getById(vendor.getAgentId());
				if (dbEthAgent != null) {
					ObjectUtil.copyProperties(vendor, dbEthAgent,
							new String[] { "createdBy", "creationDate", "agentId", "enabledFlag" });
				}
				ObjectUtil.updateBaseInfo(dbEthAgent, context);
				dao.update(dbEthAgent);
			} catch (Exception e) {
				logger.error("拷贝对象错误！", e);
				return CommonResult.makeFail("拷贝对象错误！");
			}
		}
		return CommonResult.makeSuccess();
	}

	private CommonResult check(EthAgent vendor) {
		if (!ObjectUtil.hasText(vendor.getAgentCode())) {
			return CommonResult.makeFail("编码不能为空！");
		}
		if (!ObjectUtil.hasText(vendor.getAgentName())) {
			return CommonResult.makeFail("名字不能为空！");
		}
		return CommonResult.makeSuccess();
	}

}
